String.prototype.trim = function() {return this.replace(/(^\s*)|(\s*$)/g, "");}
String.prototype.trimLeft = function() { return this.replace(/(^\s*)/g, ""); } 
String.prototype.trimRight = function() { return this.replace(/(\s*$)/g, ""); }
String.prototype.replaceAll = function(s1,s2){if(s1==undefined){s1=" ";}if(s2==undefined){s2="";}return this.replace(new RegExp(s1,"gm"),s2); }
String.prototype.fix=function(n){if(this.trim().length==0){return 0;}return parseFloat(parseFloat(this).toFixed(n));}
Number.prototype.fix=function(n){return parseFloat(this.toFixed(n));}
function log(txt){try{if(console){console.log(txt);}}catch(e){}}
function get(id){return document.getElementById(id);}

var regsMap=new Object();
regsMap["number"]="^[0-9]*$";
regsMap["letter"]="^[A-Za-z]+$";
regsMap["uppercase"]="^[A-Z]+$";
regsMap["lowercase"]="^[a-z]+$";
regsMap["letter-number"]="^[A-Za-z0-9]+$";
regsMap["letter-number_"]="^\w+$";
regsMap["chinese"]="^[\u4e00-\u9fa5]{0,}$";
regsMap["ID"]=/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
regsMap["qq"]='[1-9][0-9]{4,}';
regsMap["postal"]='[1-9]\d{5}(?!\d)';
regsMap["mail"]=/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
regsMap["phone"]=/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
regsMap["mobile"]="^1[\\d]{10}$";

var formValidateMessage="message",formValidateDefaultEscape=true;
function formValidate(formId,prefix)
{
	if(typeof(formId)=="object")
	{
		prefix=formId.prefix;
		formId=formId.form;
	}
	
	if(prefix==undefined){prefix="";}
	var form=get(formId);
	var ins=form.getElementsByTagName("input");
	var txts=form.getElementsByTagName("textarea");
	var sels=form.getElementsByTagName("select");
	var arr=new Array();
		for(var i=0;i<ins.length;i++)
		{
			arr.push(ins[i]);
		}
		for(var i=0;i<txts.length;i++)
		{
			arr.push(txts[i]);
		}
		for(var i=0;i<sels.length;i++)
		{
			arr.push(sels[i]);
		}
		if(get(formValidateMessage))
		{
			get(formValidateMessage).innerHTML='';
		}
		for(var i=0;i<arr.length;i++)
		{
			var ele=arr[i];
			var readonly=ele.getAttribute("readonly");
			var disabled=ele.disabled;
			if(("readonly" == readonly)||(disabled==true)){continue;}
			
			var require=ele.getAttribute("require");
			var length=ele.getAttribute(prefix+"length");
			var max=ele.getAttribute(prefix+"max");
			var min=ele.getAttribute(prefix+"min");
			var reg=ele.getAttribute(prefix+"reg");
			var regtype=ele.getAttribute(prefix+"regtype");
			var funcName=ele.getAttribute(prefix+"function");
			var message=ele.getAttribute(prefix+"message");
			var escape=ele.getAttribute("escape");
			var nochars=ele.getAttribute("nochars");
			var contains=ele.getAttribute("contains");
			
			if((length==undefined)||("null" == length)||(null == length)||("" == length)){length=ele.getAttribute("length");}
			if((max==undefined)||("null" == max)||(null == max)||("" == max)){max=ele.getAttribute("max");}
			if((min==undefined)||("null" == min)||(null == min)||("" == min)){min=ele.getAttribute("min");}
			if((reg==undefined)||("null" == reg)||(null == reg)||("" == reg)){reg=ele.getAttribute("reg");}
			if((funcName==undefined)||("null" == funcName)||(null == funcName)||("" == funcName)){funcName=ele.getAttribute("function");}
			if((regtype==undefined)||("null" == regtype)||(null == regtype)||("" == regtype)){regtype=ele.getAttribute("regtype");}
			if((message==undefined)||("null" == message)||(null == message)||("" == message)){message=ele.getAttribute("message");}
			
			if(('true' == require)||(true == require))
			{
				var value=ele.value+"";
				value=value.trimLeft().trimRight();
				//escape
				if((escape == true)||formValidateDefaultEscape)
				{
					try
					{
						value=value.replaceAll(">","&gt;").replaceAll("<","&lt;").replaceAll("\"","&quot;");
					}catch(e)
					{
						log("escape error "+e.message);
					}
				}
				ele.value=value;
				
				log("prefix="+prefix+",length="+length+",max="+max+",min="+min+",reg="+reg+",funcName="+funcName+",regtype="+regtype+",message="+message+",escape="+escape+",nochars="+nochars+",contains="+contains);
				//length
				if(length != undefined)
				{
					var ls=length.split(",");
					var has=false;
					for(var j=0;j<ls.length;j++)
					{
						if(value.length == ls[j] )
						{
							has=true;
							break;
						}
					}
					if(!has)
					{
						log("length error value.length="+value.length);
						showFormValidateErrorMessage(message);
						return false;
					}
				}
				//min
				if(min != undefined)
				{
					if(value.length < min )
					{
						log("min error value.length="+value.length);
						showFormValidateErrorMessage(message);
						return false;
					}
				}
				//max
				if(max != undefined)
				{
					if(value.length > max )
					{
						log("max error value.length="+value.length);
						showFormValidateErrorMessage(message);
						return false;
					}
				}
				//nochars
				if(nochars != undefined)
				{
					if(nochars.length > 0 )
					{
						for(var n=0;n<nochars.length;n++)
						{
							if(value.indexOf(nochars.charAt(n)+"")>=0)
							{
								log("nochars error value="+value);
								showFormValidateErrorMessage(message);
								return false;
							}
						}
					}
				}
				//contains
				if(contains != undefined)
				{
					if(contains.length > 0 )
					{
						var hasContains=false;
						for(var n=0;n<contains.length;n++)
						{
							if(value.indexOf(contains.charAt(n)+"")>=0)
							{
								hasContains=true;
								break;
							}
						}
						if(!hasContains)
						{
							log("contains error value="+value);
							showFormValidateErrorMessage(message);
							return false;
						}
					}
				}
				//RegExp
				if(reg != undefined)
				{
					var regexp=new RegExp(reg);
					if(regexp.test(value) == false )
					{
						log("reg error");
						showFormValidateErrorMessage(message);
						return false;
					}
				}
				//function
				if(funcName != undefined)
				{
					funcName=funcName.replace("(","").replace(")","").replace(" ","");
					try {
							if (typeof(eval(funcName)) == "function") 
							{
								log(funcName+" is a function ");
								var fn=eval(funcName);
								var flag=fn(value);
								log(funcName+" flag="+flag);
								if(flag==false)
								{
									showFormValidateErrorMessage(message);
									return false;
								}
							}else
							{
								log(funcName+" is not function ");
							}
						} catch(e) {log("error "+funcName+" is not a function "+e.message);}
				}
				
				//regtype
				if(regtype != undefined)
				{
					var rts=regtype.split(",");
					for(var n=0;n<rts.length;n++)
					{
						regtype=rts[n];
						if((regtype!=undefined)&&(regtype.trim().length>0))
						{
							//value range
							if("range"==regtype)
							{
								var ranges=ele.getAttribute(prefix+"range").split("-");
								if(ranges[0].trim().length > 0)
								{
									if(parseFloat(value.trim()) < parseFloat(ranges[0]))
									{
										log("range min error value="+value+",range.min="+ranges[0]);
										showFormValidateErrorMessage(message);
										return false;
									}
								}
								if(ranges[1].trim().length > 0)
								{
									if(parseFloat(value.trim()) > parseFloat(ranges[1]))
									{
										log("range max error value="+value+",range.max="+ranges[1]);
										showFormValidateErrorMessage(message);
										return false;
									}
								}
							}
							
							var regTypeExp=regsMap[regtype];
							if(regTypeExp != undefined)
							{
								var regexp=new RegExp(regTypeExp);
								if(regexp.test(value) == false )
								{
									log("regtype "+regtype+" error");
									showFormValidateErrorMessage(message);
									return false;
								}
							}else
							{
								showFormValidateErrorMessage("表达式类型"+regtype+"不存在");
								return false;
							}
						}
					}
				}
			}
		}//endfor
		return true;
}

function showFormValidateErrorMessage(message)
{
	if(get(formValidateMessage))
	{
		get(formValidateMessage).innerHTML='<font color=red>'+message+'</font>';
	}else
	{
		alert(message);
	}
}

